<template>
  <div class="user-learning">
    <!-- 学习统计 -->
    <el-row :gutter="20" class="statistics">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="stat-header">
              <el-icon><Clock /></el-icon>
              <span>学习时长</span>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.totalHours }}</div>
            <div class="stat-label">小时</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="stat-header">
              <el-icon><Collection /></el-icon>
              <span>已学课程</span>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.courseCount }}</div>
            <div class="stat-label">门</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="stat-header">
              <el-icon><Medal /></el-icon>
              <span>获得证书</span>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.certificateCount }}</div>
            <div class="stat-label">张</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="stat-header">
              <el-icon><Star /></el-icon>
              <span>学习积分</span>
            </div>
          </template>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.points }}</div>
            <div class="stat-label">分</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 学习进度 -->
    <el-card class="learning-progress">
      <template #header>
        <div class="card-header">
          <h3>学习进度</h3>
          <el-radio-group v-model="progressFilter" size="small">
            <el-radio-button label="all">全部</el-radio-button>
            <el-radio-button label="in-progress">进行中</el-radio-button>
            <el-radio-button label="completed">已完成</el-radio-button>
          </el-radio-group>
        </div>
      </template>

      <el-table :data="filteredCourses" style="width: 100%">
        <el-table-column label="课程信息" min-width="400">
          <template #default="{ row }">
            <div class="course-info">
              <el-image
                :src="row.cover"
                class="course-cover"
                fit="cover"
              />
              <div class="course-detail">
                <h4>{{ row.title }}</h4>
                <p>{{ row.description }}</p>
                <div class="course-meta">
                  <span>
                    <el-icon><User /></el-icon>
                    {{ row.teacher }}
                  </span>
                  <span>
                    <el-icon><Clock /></el-icon>
                    {{ row.duration }}课时
                  </span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="学习进度" width="200">
          <template #default="{ row }">
            <div class="progress-info">
              <el-progress
                :percentage="row.progress"
                :status="row.progress === 100 ? 'success' : ''"
              />
              <span class="progress-text">{{ row.progress }}%</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="最近学习" width="180">
          <template #default="{ row }">
            <div class="last-study">
              <div>{{ row.lastStudyTime }}</div>
              <div class="last-lesson">{{ row.lastLesson }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="continueLearning(row)"
            >
              继续学习
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import {
  Clock,
  Collection,
  Medal,
  Star,
  User
} from '@element-plus/icons-vue'

const router = useRouter()

// 统计数据
const statistics = ref({
  totalHours: 128,
  courseCount: 12,
  certificateCount: 5,
  points: 2560
})

// 进度筛选
const progressFilter = ref('all')

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 课程数据
const courses = ref([
  {
    id: 1,
    title: 'Vue.js 3.0 实战教程',
    description: '从入门到精通的 Vue.js 3.0 课程',
    cover: 'https://picsum.photos/200/200?random=1',
    teacher: '张老师',
    duration: 24,
    progress: 75,
    lastStudyTime: '2024-03-15 14:30',
    lastLesson: '第15课：Vue Router 实战',
    status: 'in-progress'
  },
  {
    id: 2,
    title: 'React 18 新特性详解',
    description: '深入理解 React 18 的新特性和最佳实践',
    cover: 'https://picsum.photos/200/200?random=2',
    teacher: '李老师',
    duration: 18,
    progress: 100,
    lastStudyTime: '2024-03-14 16:45',
    lastLesson: '第18课：课程总结',
    status: 'completed'
  }
])

// 根据筛选条件过滤课程
const filteredCourses = computed(() => {
  if (progressFilter.value === 'all') {
    return courses.value
  }
  return courses.value.filter(course => course.status === progressFilter.value)
})

// 继续学习
const continueLearning = (course) => {
  router.push(`/course/${course.id}`)
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped>
.user-learning {
  padding: 20px;
}

.statistics {
  margin-bottom: 20px;
}

.stat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--el-text-color-secondary);
}

.stat-content {
  text-align: center;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: var(--el-color-primary);
  line-height: 1.2;
}

.stat-label {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-top: 4px;
}

.learning-progress {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
}

.course-info {
  display: flex;
  gap: 16px;
}

.course-cover {
  width: 120px;
  height: 80px;
  border-radius: 4px;
}

.course-detail {
  flex: 1;
}

.course-detail h4 {
  margin: 0 0 8px;
  font-size: 16px;
}

.course-detail p {
  margin: 0 0 8px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.course-meta {
  display: flex;
  gap: 16px;
  color: var(--el-text-color-secondary);
  font-size: 13px;
}

.course-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.progress-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-text {
  min-width: 40px;
  text-align: right;
  color: var(--el-text-color-secondary);
}

.last-study {
  color: var(--el-text-color-secondary);
  font-size: 13px;
}

.last-lesson {
  margin-top: 4px;
  color: var(--el-text-color-regular);
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 